
package com.microweb.toastmaster.dao.hibernate;

import java.util.List;

import com.microweb.toastmaster.dao.hibernate.BaseDaoHibernate;
import com.microweb.toastmaster.model.TMProject;
import com.microweb.toastmaster.dao.TMProjectDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class TMProjectDaoHibernate extends BaseDaoHibernate implements TMProjectDao {

    /**
     * @see com.microweb.toastmaster.dao.TMProjectDao#getTMProjects(com.microweb.toastmaster.model.TMProject)
     */
    public List getTMProjects(final TMProject tMProject) {
        return getHibernateTemplate().find("from TMProject order by number");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.
        if (tMProject == null) {
            return getHibernateTemplate().find("from TMProject");
        } else {
            // filter on properties set in the tMProject
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(tMProject).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(TMProject.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }*/
    }

    /**
     * @see com.microweb.toastmaster.dao.TMProjectDao#getTMProject(Long id)
     */
    public TMProject getTMProject(final Long id) {
        TMProject tMProject = (TMProject) getHibernateTemplate().get(TMProject.class, id);
        if (tMProject == null) {
            log.warn("uh oh, tMProject with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(TMProject.class, id);
        }

        return tMProject;
    }

    /**
     * @see com.microweb.toastmaster.dao.TMProjectDao#saveTMProject(TMProject tMProject)
     */    
    public void saveTMProject(final TMProject tMProject) {
        getHibernateTemplate().saveOrUpdate(tMProject);
    }

    /**
     * @see com.microweb.toastmaster.dao.TMProjectDao#removeTMProject(Long id)
     */
    public void removeTMProject(final Long id) {
        getHibernateTemplate().delete(getTMProject(id));
    }
}
